package com.aviation.sell_ticket.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.aviation.sell_ticket.mapper.OrderDetailsMapper;
import com.aviation.sell_ticket.pojo.Aviation_model_seat;
import com.aviation.sell_ticket.pojo.Aviation_order_details;
import com.aviation.sell_ticket.service.ModelSeatService;
import com.aviation.sell_ticket.service.OrderDetailsService;
import com.aviation.sell_ticket.tool.RedisUtil;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.*;

@Service
public class OrderDetailsServiceImpl implements OrderDetailsService {

    @Resource
    OrderDetailsMapper orderDetailsMapper;

    @Resource
    ModelSeatService modelSeatService;

    @Resource
    private RedisUtil redis;//引入redis


    /**
     * 查询此列航班未被购买的座位id
     * @param flightId 航班id
     * @param departureDate 出发时间
     * @param modelId //机型id
     * @return List<String>
     */
    @Override
    public List<String> queryOrderDetails(String flightId, LocalDate departureDate,Integer modelId,Integer cabinClass) {

        //从数据库提取数据
        List<String> details =orderDetailsMapper.queryOrderDetails(flightId,departureDate);

        //取出此机型所有的航班座位
        List<Aviation_model_seat> modelSeats=modelSeatService.querySeat(modelId);

        //创建没有被选择的座位Id集合
        List<String> noSeatsSelected = new ArrayList<>();

        Set<String> keys=redis.getKeys(flightId+departureDate+"seat:*");

        //筛选没有被购买的座位
        for (Aviation_model_seat seat : modelSeats) {
            if(Objects.equals(seat.getCabin_class(), cabinClass)) {
                boolean isAdd = true;
                for (String key : keys) {
                    String value= StringUtils.substringAfter(key, ":");
                    if (seat.getSeat_id().equals(value)){
                        isAdd= false;
                        break;
                    }
                }
                if(!isAdd){
                    continue;
                }
                for (String detail : details) {
                    if(detail.equals(seat.getSeat_id())){
                        isAdd= false;
                        break;
                    }
                }
                if(isAdd){
                    noSeatsSelected.add(seat.getSeat_id());
                }
            }
        }

        return noSeatsSelected;
    }

    /**
     * 新增行程信息
     * @param detailsList 行程信息
     * @return Boolean
     */
    @Override
    public Boolean InsertOrderDetails(Aviation_order_details detailsList) {
        return orderDetailsMapper.InsertOrderDetails(detailsList)>0;
    }

    /**
     * 查询订单下所有详细信息
     * @param orderId 订单id
     * @return List<Aviation_order_details>
     */
    @Override
    public List<Aviation_order_details> detailedOrder(String orderId) {
        return orderDetailsMapper.detailedOrder(orderId);
    }

    /**
     * 查询乘机人是否购买同一趟订单
     * @param passengerName 乘机人姓名
     * @param passengerNumber 乘机人身份证号
     * @param flightId 航班id
     * @param departureDate 乘机日期
     * @return Boolean
     */
    @Override
    public Aviation_order_details sameOrder(String passengerName, String passengerNumber, String flightId, LocalDate departureDate) {
        return orderDetailsMapper.sameOrder(passengerName,passengerNumber,flightId,departureDate);
    }
}
